package com.daxia.ogmall.web.controller.admin;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daxia.ogmall.dto.JsonResultDTO;
import com.daxia.ogmall.dto.ProductCategoryTagDTO;
import com.daxia.ogmall.model.ProductCategory;
import com.daxia.ogmall.service.ProductCategoryTagService;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/productCategoryTag", produces="text/html;charset=UTF-8")
public class AdminProductCategoryTagController extends BaseController {

	@Autowired
	private ProductCategoryTagService productCategoryTagService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long productCategoryId) {
		map.put("productCategoryId", productCategoryId);
		ProductCategoryTagDTO query = new ProductCategoryTagDTO();
		query.setProductCategory(new ProductCategory());
		query.getProductCategory().setId(productCategoryId);
		List<ProductCategoryTagDTO> tags = productCategoryTagService.find(query, null);
		map.put("tags", tags);
		return "admin/productCategoryTag/productCategoryTag_detail";
	}
	
	@RequestMapping(value = "/edit")
	public String edit(Map<String, Object> map, Long productCategoryId, Long id) {
		if (id != null) {
			map.put("n", productCategoryTagService.load(id));
		}
		map.put("productCategoryId", productCategoryId);
		return "admin/productCategoryTag/productCategoryTag_edit";
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/save")
	
	public String save(ProductCategoryTagDTO dto, JsonResultDTO resultDTO) throws Exception {
		assertTrue(StringUtils.isNotBlank(dto.getName()), "标签名不能为空");
		assertTrue(StringUtils.isNotBlank(dto.getValues()), "标签值不能为空");
		if (dto.getId() == null) {
			productCategoryTagService.create(dto);
		} else {
			productCategoryTagService.updateAllFields(dto);
		}
		return okAndClose(resultDTO);
	}

    
	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids, JsonResultDTO resultDTO) throws IOException {
		productCategoryTagService.deleteByIds(ids);
		return okAndRefresh(resultDTO);
	}
	
	@RequestMapping(value = "/list")
	public String list(ProductCategoryTagDTO dto, Map<String, Object> map, Page page) {
		List<ProductCategoryTagDTO> dtos = productCategoryTagService.find(dto, page);
		map.put("list", dtos);
		map.put("productCatgoryList", productCategoryService.findAll());
		map.put("query", dto);
		return "admin/productCategoryTag/productCategoryTag_list";
	}
	
}
